/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "LUA_Dialog.h"
#include "../gui/GUI_Manager.h"

#include "../kernel.h"

extern Kernel* g_kernel;

// our dialog box reference
//MDialogBox* LUA_Dialog::m_comp = NULL;

LUA_Dialog::LUA_Dialog( lua_State *L ) : LUA_Component(L)
{
    // retrieve the name from lua
    std::string name   = luaL_checkstring( L, 1 );
    // retrieve the potential owner
    std::string sowner = luaL_checkstring( L, 2 );
    const gui::Component* owner = gui::GUI_Manager::GetInstance()->GetParentComponent( sowner );
    
    m_comp = new gui::MDialogBox( owner ); // might be NULL
    m_comp->SetName( name );

    if ( !owner )
        gui::GUI_Manager::GetInstance()->AddComponent( m_comp );
}

LUA_Dialog::~LUA_Dialog(void)
{
}

/** Create a new dialog box */
int LUA_Dialog::Create( lua_State *L)
{
    if ( m_comp )
    {
        gui::MDialogBox* d = static_cast<gui::MDialogBox*>( m_comp );
        // retrieve the name from lua
        std::string name   = luaL_checkstring( L, 1 );
        d->SetTitle( name );
        d->SetVisible( true );
        m_comp->SetFocus( true );
        // Display the dialog box
        g_kernel->GetGame()->PushState( "DialogState", true );
    }


    return 0;
}

/** Set Title */
int LUA_Dialog::SetTitle( lua_State *L )
{
    if ( m_comp )
    {
        gui::MDialogBox* d = static_cast<gui::MDialogBox*>( m_comp );
        // retrieve the name from lua
        std::string name   = luaL_checkstring( L, 1 );
        d->SetTitle( name );
    }
    return 0;
}

/** Clear the dialog box */
int LUA_Dialog::Clear(lua_State *L)
{
    if( m_comp ) {
        gui::MDialogBox* d = static_cast<gui::MDialogBox*>( m_comp );
        d->Clear();
    }
    return 0;
}

/** Load a new dialog tree */
int LUA_Dialog::LoadDialog(lua_State *L)
{
    // retrieve the name from lua
    std::string name = luaL_checkstring( L, 1 );
    if ( m_comp )
    {
        gui::MDialogBox* d = static_cast<gui::MDialogBox*>( m_comp );
        d->Clear();
        // load the dialog file
        d->LoadDialog( name );
    }
    return 0;
}

int LUA_Dialog::SetText(lua_State *L)
{
    // retrieve the name from lua
    std::string name = luaL_checkstring( L, 1 );
    if ( m_comp )
    {
        gui::MDialogBox* d = static_cast<gui::MDialogBox*>( m_comp );
        d->Clear();
        // load the dialog file
        d->SetText( name );
    }
    return 0;
}

//// -- Bind to LUA
//#define method(name) {#name, LUA_Dialog::name}
//
//const luaL_reg LUA_Dialog::methods[] = {
//        method(Create),
//        method(Clear),
//        method(SetText),
//        method(LoadDialog),
//        {NULL, NULL}
//};
//
//// - Bind to lua
//int luaopen_LUA_Dialog(lua_State *L) {
//    luaL_openlib(L, "dialog", LUA_Dialog::methods, 0);
// 	return 0;
//}

// -- Bind to LUA
#define method(class, name) {#name, &class::##name}

const char LUA_Dialog::className[] = "GUI_Dialog";
Lunar<LUA_Dialog>::RegType LUA_Dialog::methods[] = {
        method(LUA_Dialog, Create),
        method(LUA_Dialog, Destroy),
        method(LUA_Dialog, SetTitle),
        method(LUA_Dialog, SetName),
        method(LUA_Dialog, GetName),
        method(LUA_Dialog, SetCaption),
        method(LUA_Dialog, GetCaption),
        method(LUA_Dialog, SetLocation),
        method(LUA_Dialog, GetLocation),
        method(LUA_Dialog, SetSize),
        method(LUA_Dialog, GetWidth),
        method(LUA_Dialog, GetHeight),
        method(LUA_Dialog, SetBackgroundColor),
        method(LUA_Dialog, GetBackgroundColor),
        method(LUA_Dialog, SetAlpha),
        method(LUA_Dialog, GetAlpha),
        method(LUA_Dialog, SetFocus),
        method(LUA_Dialog, HasFocus),
        method(LUA_Dialog, SetVisible),
        method(LUA_Dialog, IsVisible),

        method(LUA_Dialog, Clear),
        method(LUA_Dialog, SetText),
        method(LUA_Dialog, LoadDialog),
        {NULL, NULL}
};